/**
 * 微信公众平台开发模式(JAVA) SDK
 * (c) 2012-2014 ____′↘夏悸 <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.shixu.framework.plugins.qyweixin.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shixu.framework.plugins.qyweixin.pojo.QyUserInfo;
import com.shixu.framework.plugins.qyweixin.util.HttpKit;
import com.shixu.framework.util.JsonUtils;


/**
 * 用户操作接口
 * @author ____′↘夏悸
 */
public class QyUser {
	
	final Logger logger = LoggerFactory.getLogger(QyUser.class);	

	private static final String USER_GET_URI = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=";
	private static final String USER_CREATE_URI = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=";
	private static final String USER_UPDATE_URI = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=";
	private static final String USER_DELETE_URI = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=";
	private static final String USER_INVITE_SEND_URI = "https://qyapi.weixin.qq.com/cgi-bin/invite/send?access_token=";
	
	/**
	 * 获取成员
	 * @param accessToken
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public QyUserInfo getUserInfo(String accessToken, String userid) throws Exception {
		QyUserInfo userInfo = null;
		
		String jsonStr = HttpKit.get(USER_GET_URI.concat(accessToken) + "&userid=" + userid);
		
		if(StringUtils.isNotEmpty(jsonStr)){
			JSONObject obj = JSONObject.fromObject(jsonStr);
			if(obj.getInt("errcode") == 0){
				userInfo = JsonUtils.fromJson(jsonStr, QyUserInfo.class);
			}
		}
		return userInfo;
	}
	
	/**
	 * 创建成员
	 * @param accessToken
	 * @param qyUserInfo
	 * @return
	 * @throws Exception
	 */
	public String create(String accessToken, QyUserInfo qyUserInfo) throws Exception{
		String reslut = HttpKit.post(USER_CREATE_URI.concat(accessToken), com.alibaba.fastjson.JSONObject.toJSONString(qyUserInfo));
//		JSONObject jsonObject = JSONObject.fromObject(reslut);
		return reslut;
	}
	
	/**
	 * 更新成员
	 * @param accessToken
	 * @param qyUserInfo
	 * @return
	 * @throws Exception
	 */
	public String update(String accessToken, QyUserInfo qyUserInfo) throws Exception{
		String reslut = HttpKit.post(USER_UPDATE_URI.concat(accessToken), com.alibaba.fastjson.JSONObject.toJSONString(qyUserInfo));
		return reslut;
	}
	
	/**
	 * 删除成员
	 * @param accessToken
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public String delete(String accessToken, String userid) throws Exception {
		String jsonStr = HttpKit.get(USER_DELETE_URI.concat(accessToken) + "&userid=" + userid);
		return jsonStr;
	}
	
	/**
	 * 邀请成员
	 * @param accessToken
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public String sendInvite(String accessToken, String userid,String inviteTips) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("userid", userid);
		//params.put("invite_tips", inviteTips);
		logger.debug("----sendInvite params----"+JsonUtils.toJson(params));
		String jsonStr = HttpKit.post(USER_INVITE_SEND_URI.concat(accessToken),params);
		return jsonStr;
	}
	
}